home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapte18 / sendcmd.c < prev    next >
C/C++ Source or Header  |  1996-04-29  |  6KB  |  217 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "SendCmd.h"
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName = "MyApp";
  19. LPCTSTR lpszTitle   = "mciSendCommand()"; 
  20.  
  21. // the rest of the stuff
  22. //......................
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.     WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. #define MSG_LEN          1024
  108.  
  109. char           msg[MSG_LEN+1];
  110.  
  111. MCIERROR       rc;
  112. UINT           uDeviceId = 0;
  113. MCI_OPEN_PARMS open;
  114. MCI_PLAY_PARMS play;
  115.  
  116.  
  117. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  118. {
  119.    switch( uMsg )
  120.    {
  121.       case WM_COMMAND :
  122.               switch( LOWORD( wParam ) )
  123.               {
  124.                  case IDM_TEST:
  125.                         {
  126.                            // open waveform-audio device
  127.                            //...........................
  128.  
  129.                            open.dwCallback       = (DWORD)hWnd;
  130.                            open.lpstrDeviceType  = "waveaudio";
  131.                            open.lpstrElementName = "Sample2.wav";
  132.  
  133.                            rc = mciSendCommand(0, MCI_OPEN, 
  134.                                                MCI_OPEN_TYPE | 
  135.                                                MCI_OPEN_ELEMENT,
  136.                                                (DWORD)&open);
  137.  
  138.                            if (rc)
  139.                            {
  140.                                mciGetErrorString(rc, msg, MSG_LEN);
  141.                                MessageBox(hWnd, msg, NULL, MB_ICONSTOP|MB_OK);
  142.                                return(0);
  143.                            }
  144.  
  145.                            // play back sample2.wav
  146.                            //......................
  147.  
  148.                            uDeviceId = open.wDeviceID;
  149.                            play.dwCallback = (DWORD)hWnd;
  150.                            
  151.                            rc = mciSendCommand(uDeviceId, MCI_PLAY, MCI_NOTIFY, 
  152.                                                (DWORD)&play);
  153.                         }
  154.                         break;
  155.  
  156.                  case IDM_ABOUT :
  157.                         DialogBox( hInst, "AboutBox", hWnd, About );
  158.                         break;
  159.  
  160.                  case IDM_EXIT :
  161.                         DestroyWindow(hWnd);
  162.                         break;
  163.               }
  164.               break;
  165.  
  166.       case MM_MCINOTIFY:
  167.               // close waveform-audio device
  168.               //............................
  169.  
  170.               mciSendCommand(uDeviceId, MCI_CLOSE, 0, (DWORD)NULL);
  171.               break;
  172.  
  173.       case WM_DESTROY :
  174.               PostQuitMessage(0);
  175.               break;
  176.  
  177.       default :
  178.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  179.    }
  180.  
  181.    return( 0L );               
  182. }
  183.  
  184.  
  185. LRESULT CALLBACK About( HWND hDlg,           
  186.                         UINT message,        
  187.                         WPARAM wParam,       
  188.                         LPARAM lParam)
  189. {
  190.    switch (message) 
  191.    {
  192.        case WM_INITDIALOG: 
  193.                return (TRUE);
  194.  
  195.        case WM_COMMAND:                              
  196.                if (   LOWORD(wParam) == IDOK         
  197.                    || LOWORD(wParam) == IDCANCEL)    
  198.                {
  199.                        EndDialog(hDlg, TRUE);        
  200.                        return (TRUE);
  201.                }
  202.                break;
  203.    }
  204.  
  205.    return (FALSE); 
  206. }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.